package com.devops.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devops.admin.mapper.DOSResourceTypeMapper;
import com.devops.admin.model.DOSResourceType;
import com.devops.admin.po.DOSResourceSubTypePo;
import com.devops.admin.po.DOSSubTypePo;
import com.devops.admin.service.DOSResourceTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DOSResourceTypeServiceImpl
 * @Description TODO
 * @Author hepingguo
 * @Date 2022/6/8 11:45
 * @Version 1.0
 **/
@Service
public class DOSResourceTypeServiceImpl extends ServiceImpl<DOSResourceTypeMapper, DOSResourceType> implements DOSResourceTypeService {
    @Override
    public List<DOSResourceSubTypePo> getResourceTypes() {

        List<DOSResourceSubTypePo> resourceSubTypePos = baseMapper.selectTypes();
        Map<Integer, List<DOSResourceSubTypePo>> typeMap = resourceSubTypePos.stream().collect(Collectors.groupingBy(DOSResourceSubTypePo::getType));
        List<DOSResourceSubTypePo> resourceSubTypes = new ArrayList<>();
        typeMap.forEach((k,v) -> {
            List<DOSResourceSubTypePo> typePos = v;
            DOSResourceSubTypePo dosResourceSubTypePo = typePos.get(0);
            List<DOSSubTypePo> subTypePos = new ArrayList<>();
            typePos.forEach(type -> {
                DOSSubTypePo subTypePo = DOSSubTypePo.builder()
                        .subType(type.getSubType())
                        .subId(type.getSubId())
                        .subName(type.getSubName())
                        .subRemark(type.getSubRemark())
                        .build();
                subTypePos.add(subTypePo);
            });
            dosResourceSubTypePo.setSubTypes(subTypePos);
            resourceSubTypes.add(dosResourceSubTypePo);
        });
        return resourceSubTypes;
    }
}
